package edu.office.applicationRepository;


import edu.office.entities.Roleinfo;
import edu.office.tools.EOMessage;
import edu.office.tools.EOMessage1;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Named("roleinfoRepositoryA")
@ApplicationScoped
public class RoleinfoRepository extends ApplicationCommonRepository<Roleinfo>{

    private List<Roleinfo> allRoleinfos = new LinkedList<>();
    private final String[] roleName = {"Admin", "Secretary", "Teacher", "Student", "Parent", "Reexamin"};

    private final HashMap<String, Roleinfo> roleNameMap =new HashMap<>();

    private final HashMap<Integer,Roleinfo> roleIdMap=new HashMap<>();


    public boolean isAdmin(Integer teacher) {
        return getAdminRoleinfo().getID().equals(teacher);
    }
    
    public boolean isAdmin(Roleinfo roleType){
         return isAdmin(roleType.getID());
    }

    public boolean isSecreatary(Integer teacher) {
        return getSecretaryRoleinfo().getID().equals(teacher);
    }

    public boolean isSecreatary(Roleinfo roleType) {
        return getSecretaryRoleinfo().getID().equals(roleType.getID());
    }

    public boolean isAdminOrSecretary(Integer teacher) {
       return getAdminRoleinfo().getID().equals(teacher)
               ||getSecretaryRoleinfo().getID().equals(teacher);
    }

    public boolean isTeahcer(Integer roleType) {
      return   getAdminRoleinfo().getID().equals(roleType);
    }

    public boolean isStudent(Integer roleType) {
        return getStudentRoleinfo().getID().equals(roleType);
    }

    public boolean isParent(Integer roleType) {
        return getParentRoleinfo().getID().equals(roleType);
    }

    public boolean isReexamin(Integer roleType) {
        return getReexaminRoleinfo().getID().equals(roleType);
    }

    public List<Roleinfo> getAllList() {
        if (allRoleinfos.isEmpty()||isDataChanged()) {
            allRoleinfos = listAll();
        }
        return allRoleinfos;
    }

    public Roleinfo getRoleinfo(UserType userType) {
        return switch (userType) {
            case Student -> getStudentRoleinfo();
            case Parent -> getParentRoleinfo();
            case Teacher -> getTeacherRoleinfo();
            case Secretary -> getSecretaryRoleinfo();
            case Admin -> getAdminRoleinfo();
            case Reexamin -> getReexaminRoleinfo();
        };
    }

    public Roleinfo getStudentRoleinfo() {
return getRoleByName().get(roleName[2]);
    }

    public Roleinfo getParentRoleinfo() {
        return getRoleByName().get(roleName[4]);
    }

    public Roleinfo getTeacherRoleinfo() {
        return getRoleByName().get(roleName[2]);
    }

    public Roleinfo getAdminRoleinfo() {
        return getRoleByName().get(roleName[0]);
    }

    public Roleinfo getSecretaryRoleinfo() {
        return  getRoleByName().get(roleName[1]);
    }

    public Roleinfo getReexaminRoleinfo() {
        return  getRoleByName().get(roleName[5]);
    }
    public String[] getRoleName() {
        return roleName;
    }

    public HashMap<String, Roleinfo> getRoleByName() {
        if(roleNameMap.isEmpty()){
            List<Roleinfo> roles=getAllList();
            for(Roleinfo roleinfo:roles){
                roleNameMap.put(roleinfo.getROLENAME(),roleinfo);
            }
        }
        return roleNameMap;
    }
    public Roleinfo getRoleById(Integer id) {
        if(roleIdMap.isEmpty()){
            List<Roleinfo> roles=getAllList();
            for(Roleinfo roleinfo:roles){
                roleIdMap.put(roleinfo.getID(),roleinfo);
            }
        }
        return roleIdMap.get(id);
    }


    public String myCreate(Roleinfo roleinfo) {
        if (list("ROLENAME", roleinfo.getROLENAME()).isEmpty()) {
            create(roleinfo);
            return EOMessage.getEOMessageENUM(EOMessage1.Succeed);
        } else {
            return roleinfo.getROLENAME() + EOMessage.getEOMessageENUM(EOMessage1.Already_Exist);
        }
    }
}
